

package io.lkt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.modules.sys.dao.SysUserGzhDao;
import io.lkt.modules.sys.dao.SysUserRoleDao;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.entity.SysUserGzhEntity;
import io.lkt.modules.sys.entity.SysUserRoleEntity;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 用户与角色对应关系
 *
 * @author xss dustwas@163.com
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRoleEntity> implements SysUserRoleService {

    @Autowired
    private SysUserGzhDao sysUserGzhDao;

	@Override
	public void saveOrUpdate(Long userId, List<Long> roleIdList) {
		//先删除用户与角色关系
		this.remove(new QueryWrapper<SysUserRoleEntity>().eq("user_id", userId));

		if(roleIdList == null || roleIdList.size() == 0){
			return ;
		}

		//保存用户与角色关系
		for(Long roleId : roleIdList){
			SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
			sysUserRoleEntity.setUserId(userId);
			sysUserRoleEntity.setRoleId(roleId);

			this.save(sysUserRoleEntity);
		}

	}

    @Override
    public void saveOrUpdateGZH(Long userId, List<String> gzhCodeList) {
        //先删除用户与公众号关系
        sysUserGzhDao.deleteByUserId(userId);

        if(gzhCodeList == null || gzhCodeList.size() == 0){
            return ;
        }

        //保存用户与公众号关系
        for(String gzhCoud : gzhCodeList){
            sysUserGzhDao.insertByUserId(userId,gzhCoud);
        }
    }

    @Override
    public List<String> queryGzhCodeList(Long userId) {
        return sysUserGzhDao.queryGzhCodeList(userId);
    }


	@Override
	public List<SysUserGzhEntity> queryGzhList(Long userId){
		return sysUserGzhDao.queryGzhList(userId);
	}

    @Override
    public List<SysGzhEntity> selectGzhList(){
        return sysUserGzhDao.selectGzhList();
    }

	@Override
	public SysGzhEntity getGzhByCode(String gzhCode){
		return sysUserGzhDao.getGzhByCode(gzhCode);
	}

	@Override
	public SysGzhEntity queryGzhCode(String gzhCode){
		return sysUserGzhDao.queryGzhCode(gzhCode);
	}

	@Override
	public List<Long> queryRoleIdList(Long userId) {
		return baseMapper.queryRoleIdList(userId);
	}

	@Override
	public int deleteBatch(Long[] roleIds){
		return baseMapper.deleteBatch(roleIds);
	}
}
